package com.haoyi.feignapi.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.haoyi.feignapi.entity.erp.ErpImg;
import com.haoyi.feignapi.entity.erp.ErpOCC;
import com.haoyi.feignapi.mapper.ErpMapper;
import com.haoyi.feignapi.mapper.ErpOCCMapper;
import com.haoyi.feignapi.mapper.ImgMapper;
import com.haoyi.feignapi.service.ErpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
@DS("erp")
public class ErpServiceImpl implements ErpService {
    @Autowired
    ErpOCCMapper erpOCCMapper;

    @Autowired
    ImgMapper imgMapper;

    @Autowired
    ErpMapper erpMapper;

    @Override
    public String feignTest() {
        return erpMapper.getOcmOrderMaxCode();
    }

    @Override
    public List<String> getErpCode(List<String> search) {
        return erpMapper.getErpCode(search);
    }

    @Override
    public PageInfo<Map<String, Object>> getErpInventory(Map<String, Object> paramMap) {
        Integer currPage = (Integer) paramMap.get("currPage");
        Integer pageSize = (Integer) paramMap.get("pageSize");
        PageHelper.startPage(currPage, pageSize);
        String selectKey = "";
        if (paramMap.get("select") != null && paramMap.get("select").toString().equals("W01")) {
            selectKey = "imd01";
        } else if (paramMap.get("select") != null && paramMap.get("select").toString().equals("W02")) {
            selectKey = "imd02";
        }
        if (paramMap.get("search") != null && !paramMap.get("search").toString().isEmpty()) {
            paramMap.put("search", "%" + paramMap.get("search").toString() + "%");
        }
        paramMap.put("selectKey", selectKey);
        List<Map<String, Object>> erpPartsList = erpMapper.getErpInventory(paramMap);
        PageInfo<Map<String, Object>> resultData = new PageInfo<>(erpPartsList);
        return resultData;
    }

    @Override
    public PageInfo<Map<String, Object>> getErpParts(Map<String, Object> paramMap) {
        Integer currPage = (Integer) paramMap.get("currPage");
        Integer pageSize = (Integer) paramMap.get("pageSize");
        PageHelper.startPage(currPage, pageSize);
        List<Map<String, Object>> erpPartsList = erpMapper.getErpParts(paramMap);
        PageInfo<Map<String, Object>> resultData = new PageInfo<>(erpPartsList);
        return resultData;
    }

    @Override
    public List<Map<String, Object>> getOrderErpCode(Map<String, Object> paramMap) {
        return erpMapper.getOrderErpCode((List<String>) paramMap.get("ocm_code_list"));
    }

    @Override
    public ErpOCC getCode(String company) {
        ErpOCC erpOCC = new ErpOCC();
        if (company!=null){
            erpOCC = erpOCCMapper.getCode(company);
        }
        return erpOCC;
    }

    @Override
    public ArrayList<ErpImg> allMaterial() {
        ArrayList<ErpImg> imgs = imgMapper.allMaterial();
        return imgs;
    }

    @Override
    public ErpImg different(String item, String item1) {
        ErpImg different = imgMapper.different(item,item1);
        return different;
    }


}
